﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Reflection;
using System.Drawing.Design;

namespace ProgNetComponents.Design.Designers.ActionLists
{
    /// <summary>
    /// Action list for editing collection items
    /// </summary>
    public class EditItemsActionList : DesignerActionList
    {
        /// <summary>
        /// Collection of DesignerActionItems
        /// </summary>
        protected DesignerActionItemCollection list = new DesignerActionItemCollection();
        /// <summary>
        /// IDesigner
        /// </summary>
        protected IDesigner _Designer;
        /// <summary>
        /// Owner
        /// </summary>
        protected object _Owner;
        /// <summary>
        /// Property name
        /// </summary>
        protected string _PropertyName;
        /// <summary>
        /// Creates EditItemsActionList instance
        /// </summary>
        /// <param name="designer"></param>
        /// <param name="owner"></param>
        /// <param name="propertyName"></param>
        public EditItemsActionList(IDesigner designer, object owner, string propertyName = "Items")
            : base(designer.Component)
        {
            _Designer = designer;
            _Owner = owner;
            _PropertyName = propertyName;

            list.Add(new DesignerActionMethodItem(this, "EditItems", MethodDisplayName));
        }
        /// <summary>
        /// Gets Display name for Action item
        /// </summary>
        protected virtual string MethodDisplayName
        {
            get { return "Edit " + _PropertyName; }
        }
        /// <summary>
        /// Show edit items dialog
        /// </summary>
        public void EditItems()
        {
            try
            {
                PropertyDescriptor prop = TypeDescriptor.GetProperties(_Owner)[_PropertyName];
                UITypeEditor editor = prop.GetEditor(typeof(UITypeEditor)) as UITypeEditor;

                if (editor != null && _Designer != null)
                {
                    object obj1 = prop.GetValue(_Owner);
                    ContextHelper ch = new ContextHelper(_Designer, prop);
                    object obj2 = editor.EditValue(ch, ch, obj1);
                    if (obj1 != obj2)
                    {
                        try
                        {
                            prop.SetValue(_Owner, obj2);
                        }
                        catch { }
                    }
                }
            }
            catch 
            {
                
            }
        }
        /// <summary>
        /// Returns collection of DesignerActionItems
        /// </summary>
        /// <returns></returns>
        public override DesignerActionItemCollection GetSortedActionItems()
        {
            return list;
        }
    }
}
